package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.data.entity.UserInfoEntity;
import com.cheng.data.vo.UserInfoVO;
import com.cheng.operation.LoginOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author: ChengJW
 * @date: 2021/11/04 21:11
 * @version: 1.0
 * @description: 登录操作相关逻辑处理
 */
@Service
public class LoginOperationImpl implements LoginOperation {

    private final static Logger log = LoggerFactory.getLogger(LoginOperationImpl.class);

    /**
    * @author: ChengJW
    * @params: [userInfoEntity]
    * @return: com.cheng.data.vo.UserInfoVO
    * @date: 2021/11/04
    * @description: shiro 登录身份验证
    */
    @Override
    public UserInfoVO userLogin(UserInfoEntity userInfoEntity) throws CommonException {

        if (null == userInfoEntity){
            log.error("LoginOperationImpl-userLogin-exception:userInfoEntity is null when client login");
            throw new CommonException("CJW202111010021","LoginOperationImpl","userLogin");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(userInfoEntity.getUrNm(),userInfoEntity.getUrPwd());
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        UserInfoVO userInfoVO = null;
        if (subject.isAuthenticated()){
            userInfoVO = (UserInfoVO)subject.getPrincipal();
        }
        return userInfoVO;
    }
}
